import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:lovify/core/constants/config.dart';
import 'package:lovify/core/network/file_service.dart';
import 'package:lovify/core/network/model/response_entity.dart';
import 'package:lovify/core/network/user_service.dart';
import 'package:lovify/core/utils/Prefs.dart';
import 'package:lovify/core/utils/devices.dart';
import 'package:lovify/core/utils/geolocator.dart';
import 'package:lovify/core/utils/image.dart';
import 'package:lovify/models/enums/gender.dart';
import 'package:lovify/models/user.dart';
import 'package:lovify/state/base_controller.dart';

class UserController extends BaseController {
  var user = User().obs;
  final UserService _us = UserService();
  final FileService _fs = FileService();

  Future<bool> initialize() async {
    var u = await getUserByLocal();
    if (u == null) {
      return await _initUser();
    } else {
      user.value = u as User;
      return true;
    }
  }

  void setName(dynamic value) {
    user.update((val) {
      val?.name = value;
    });
  }

  void setBirthday(dynamic value) {
    user.update((val) {
      val?.birthday = value;
    });
  }

  String? getAvatar() {
    return user.value.avatar;
  }

  void setGender(dynamic value) {
    user.update((val) {
      val?.gender = value;
    });
  }

  Future<void> uploadAvatar() async {
    var imgFile = await ImageUtils.pickImageFromGallery();
    if (imgFile != null) {
      ResponseEntity<String> result = await _fs.upload(imgFile);
      bool checked = checkResponse(result);
      if (checked) {
        user.update((val) {
          dynamic url = result.data;
          val?.avatar = previewUrl + url;
        });
      }
    }
  }

  Future<bool> saved() async {
    if (user.value.name == null || user.value.name!.isEmpty) {
      showMsg("请输入你的姓名");
    } else if (user.value.avatar == null || user.value.avatar!.isEmpty) {
      showMsg("请上传你的个人头像");
    } else if (user.value.birthday == null) {
      showMsg("请选择你的生日");
    } else {
      var pos = await GeoUtils.getPosition();
      if (pos[0] == 0) {
        user.value.gender ??= Gender.male;
        user.value.latitude = pos[1];
        user.value.longitude = pos[2];
        User copie = User.fromJson(user.value.toJson());
        copie.avatar = copie.avatar?.replaceAll(previewUrl, "");
        ResponseEntity<User> result = await _us.create(copie);
        bool checked = checkResponse(result);
        if (checked) {
          User u = result.data as User;
          PrefsUtils.saveJSON(keyUser, u.toJson());
        }
        return checked;
      } else {
        var message = GeoUtils.getMessage(pos[0]);
        showMsg(message);
      }
    }
    return false;
  }

  Future<bool> _initUser() async {
    var deviceId = await DeviceUtils().getId();
    if (deviceId != null) {
      var u = await _getUser(deviceId);
      if (u != null) {
        user.value = u as User;
        PrefsUtils.saveJSON(keyUser, user.value.toJson());
        return true;
      } else {
        user.update((val) {
          val?.deviceId = deviceId;
        });
        return false;
      }
    }
    return false;
  }

  Future<dynamic> _getUser(String deviceId) async {
    ResponseEntity<User> result = await _us.getUserByDeviceId(deviceId);
    bool checked = checkResponse(result);
    if (checked) {
      return result.data as User;
    }
    return null;
  }

  Future<dynamic> getUserByLocal() async {
    var user = await PrefsUtils.fromJSON(keyUser);
    if (user == null) {
      return null;
    }
    return User.fromJson(user);
  }

}
